package com.jia;

import com.jia.annotation.CustomConditionalOnClass;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

public class CustomOnClassConditional implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        try {
            Map<String, Object> annotationAttributes
                    = metadata.getAnnotationAttributes(CustomConditionalOnClass.class.getName());
            String className = (String) annotationAttributes.get("value");
            context.getClassLoader().loadClass(className);
            return true;
        } catch (Exception e) {
            return false;
        }

    }

}
